package com.cn.ZhiKeDianShang.util.ImageCacheUtils;

import android.graphics.Bitmap;
import android.widget.ImageView;


/**
 * 图片三级缓存
 */
public class MyBitmapUtils {


    private NetCacheUtils mNetCacheUtils;
    private LocalCacheUtils mLocalCacheUtils;
    private MemoryCacheUtils mMemoryCacheUtils;

    public static volatile  MyBitmapUtils myBitmapUtils;


    public static MyBitmapUtils getInstance() {
        if (myBitmapUtils == null) {
            synchronized (MyBitmapUtils.class) {
                if (myBitmapUtils == null) {
                    myBitmapUtils = new MyBitmapUtils();
                }
            }
        }
        return myBitmapUtils;
    }


    public MyBitmapUtils() {
        mMemoryCacheUtils = new MemoryCacheUtils();
        mLocalCacheUtils = new LocalCacheUtils();
        mNetCacheUtils = new NetCacheUtils(mLocalCacheUtils, mMemoryCacheUtils);
    }

    public void disPlay(ImageView ivPic, String url) {
//        ivPic.setImageResource(R.drawable.anim_cancel_collect);
        if (ivPic==null||url==null){
//            SDToast.showToast("控件或是 图片地址有问题-----");
           return;
        }
        Bitmap bitmap;
        //内存缓存
        bitmap = mMemoryCacheUtils.getBitmapFromMemory(url);
        if (bitmap != null) {
            ivPic.setImageBitmap(bitmap);
            System.out.println("从内存获取图片啦.....");
            return;
        }

        //本地缓存
        bitmap = mLocalCacheUtils.getBitmapFromLocal(url);
        if (bitmap != null) {
            ivPic.setImageBitmap(bitmap);
            System.out.println("从本地获取图片啦.....");
            //从本地获取图片后,保存至内存中
            mMemoryCacheUtils.setBitmapToMemory(url, bitmap);
            return;
        }
        //网络缓存
        mNetCacheUtils.getBitmapFromNet(ivPic, url);
    }

}
